package org.anderground.editor.gui.toolbar;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JToolBar;

import org.anderground.editor.gui.MainWindow;
import org.anderground.editor.gui.menubar.OpenFileMenuItem;
import org.anderground.editor.gui.menubar.SaveMapMenuItem;
import org.anderground.editor.model.EditorModel;

public class FileToolBar extends JToolBar {
	public FileToolBar(MainWindow parent) {
		add(new NewMapButton());
		add(new OpenMapButton(parent));
		add(new SaveMapButton());
	}

	class OpenMapButton extends JButton {
		OpenMapButton(final MainWindow parent) {
			super("Open");
			setFocusable(false);
			addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					OpenFileMenuItem.showOpenFileDialog(FileToolBar.this);
					parent.getMainPanel().getFirstView().afterPanelInit();
					parent.getMainPanel().getSecondView().afterPanelInit();
				}

			});
		}
	}

	class SaveMapButton extends JButton {
		SaveMapButton() {
			super("Save");
			setFocusable(false);
			addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					SaveMapMenuItem.showSaveFileDialog(FileToolBar.this);
				}

			});
		}
	}

	class NewMapButton extends JButton {
		NewMapButton() {
			super("New map");
			setFocusable(false);
			addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					if (MainWindow
							.askForSave(
									"Do you want to crate new file without saving current?",
									NewMapButton.this) != JOptionPane.CANCEL_OPTION)
						EditorModel.getInstance().clear();

				}

			});
		}
	}

}
